﻿<div class="k-block divToolbar" style="margin: 5px 5px;">
    <input type="text" placeholder="请输入单据号" id="txtKey" class="k-textbox" />
    <div style="display: inline-block;">
        <input id="txtOrderDateBefore" class="date" placeholder="开始时间" />
    </div>
    --
    <div style="display: inline-block;">
        <input id="txtOrderDateAfter" class="date" placeholder="结束时间" />
    </div>
    <div style="display: inline-block;">
        <select id="ddlMethod" class="sel">
            <option value="">全部单据</option>
            <option value="1">采购单</option>
            <option value="2">销售单</option>
        </select>
    </div>
    <br />
    <br />
    <div style="display: inline-block;">
        <select id="ddlOrderStatus" multiple="multiple" data-placeholder="请选择单据状态" style="width: 200px;">
            <option value="1">未开始</option>
            <option value="3">进行中</option>
            <option value="5">已完成</option>
        </select>
    </div>
    <div style="display: inline-block; vertical-align: top">
        <input id="txtCustomer" data-value="0" style="width: 300px" />
    </div>
    <div style="display: inline-block; vertical-align: top">
        <input id="txtProduct" data-value="0" style="width: 300px" />
    </div>
    <br />
    <br />
    <a class="k-button" onclick="FilterOrders()"><span class="k-icon k-i-search"></span><span>搜索</span></a>
    <a class="k-button" onclick="ExportOrders()"><span class="k-icon k-i-calendar"></span><span>导出单据</span></a>
</div>
<div class="ld-tabs">
    <ul>
        <li class="k-state-active">单据列表</li>
        <li>明细列表</li>
    </ul>
    <div>
        <div id="grid_orders" style="text-align: center"></div>
        <form id="dealForm" method="post" action="/Orders/DealOrder/Export" target="_blank" style="display: none">
            <input type="hidden" name="orderNo" id="df_OrderNo" />
            <input type="hidden" name="customerID" id="df_CustomerID" />
            <input type="hidden" name="orderMethod" id="df_OrderMethod" />
            <input type="hidden" name="orderDateBefore" id="df_OrderDateBefore" />
            <input type="hidden" name="orderDateAfter" id="df_OrderDateAfter" />
            <input type="hidden" name="orderStatus" id="df_OrderStatus" />
            <input type="hidden" name="productID" id="df_ProductID" />
        </form>
    </div>
    <div>
        <div id="grid_details" style="text-align: center"></div>
    </div>
</div>

<script class="init">
    LocatedMenu("交易管理", "单据查询");

    $(".sel").kendoDropDownList({
        change: function (e) {
            FilterOrders();
        }
    });

    $("#ddlOrderStatus").kendoMultiSelect();

    $("#txtCustomer").kendoComboBox({
        placeholder: "请选择客户...",
        filter: "contains",
        dataTextField: "Pinyin",
        template: "#=ComplexName#",
        dataValueField: "ID",
        dataSource: Whale.dataSource({
            url: "/Members/Customer/Gets",
            getParameters: function () {
                return {
                    key: $.trim($("#txtCustomer").data("kendoComboBox").text())
                };
            },
        }),
        change: function (e) {
            if ($.isNumeric(this.value())) {
                var item = this.dataItem();
                $("#txtCustomer").attr("data-value", item.ID);
                this.text(item.ComplexName);
            } else if (this.text() == "") {
                $("#txtCustomer").attr("data-value", 0);
            }
        }
    });

    $(".date").kendoDatePicker({
        culture: "zh-CHS",
        format: "yyyy年MM月dd日",
    });

    function ExportOrders() {
        $("#df_OrderNo").val($("#txtKey").val());
        $("#df_CustomerID").val($("#txtCustomer").attr("data-value"));
        $("#df_OrderDateBefore").val($("#txtOrderDateBefore").val());
        $("#df_OrderDateAfter").val($("#txtOrderDateAfter").val());
        $("#df_OrderMethod").val($("#ddlMethod").data("kendoDropDownList").value());
        $("#df_OrderStatus").val($("#ddlOrderStatus").data("kendoMultiSelect").value().join(','));
        $("#df_ProductID").val($("#txtProduct").attr("data-value"));
        $("#dealForm").submit();
    }

    var allProducts = Whale.Remote.GetEntity({
        url: "/Products/Product/Gets",
        parameters: {
            pageSize: 0,
            productStatus: 1
        }
    });

    $("#txtProduct").kendoComboBox({
        placeholder: "请选择产品...",
        autoBind: false,
        filter: "contains",
        dataTextField: "Pinyin",
        template: "#=ProductName#",
        dataValueField: "ID",
        dataSource: allProducts,
        change: function (e) {
            if ($.isNumeric(this.value())) {
                var item = this.dataItem();
                $("#txtProduct").attr("data-value", item.ID);
                this.text(item.ComplexName);
            } else if (this.text() == "") {
                $("#txtProduct").attr("data-value", 0);
            }
        }
    })
</script>

<script class="grid_orders_columns">
    var ordersFields = [{
        title: "单据号",
        field: "OrderNo",
        template: "#=GetOperations(OrderNo)#",
        width: 140
    }, {
        title: "客户",
        field: "Customer.Corporation",
        width: 300
    }, {
        title: "总金额",
        field: "TotalMoney",
        format: "{0:c}",
        width: 150,
        attributes: { style: "text-align:right" }
    }, {
        title: "状态",
        field: "OrderStatus",
        template: "#=GetOrderStatusString(OrderStatus,Method)#",
        width: 100
    }, {
        title: "日期",
        template: "#=kendo.toString(kendo.parseDate(NoteDate),'yyyy-MM-dd')#",
        width: 100
    }, {}];

    function GetOperations(orderNo) {
        var html = "<a href=\"javascript:ShowBuyOrder('" + orderNo + "')\">" + orderNo + "</a>";
        return html;
    }

    function ShowBuyOrder(orderNo) {
        if (wndGeneralEdit != null && typeof wndGeneralEdit != "undefined") {
            wndGeneralEdit.setOptions({ title: "单据详情", width: 800 });
            wndGeneralEdit.refresh({ url: "/Orders/DealOrder/Order/" + orderNo });
            wndGeneralEdit.open();
        }
    }
</script>

<script class="grid_config">
    $(function () {
        $("#grid_orders").kendoGrid({
            columns: ordersFields,
            dataSource: Whale.dataSource({
                url: "/Orders/DealOrder/GetOrders",
                getParameters: function () {
                    return {
                        getCustomer: true,
                        customerID: $("#txtCustomer").attr("data-value"),
                        orderDateBefore: $("#txtOrderDateBefore").val(),
                        orderDateAfter: $("#txtOrderDateAfter").val(),
                        orderNo: $("#txtKey").val(),
                        orderMethod: $("#ddlMethod").data("kendoDropDownList").value(),
                        orderStatus: $("#ddlOrderStatus").data("kendoMultiSelect").value().join(','),
                        productID: $("#txtProduct").attr("data-value")
                    };
                },
                pageSize: 20
            }),
            pageable: {
                messages: {
                    empty: "暂无数据"
                }
            },
            dataBound: function (e) {
                //让表头居中
                $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            }
        });
    });

    function FilterOrders() {
        $("#grid_orders").data("kendoGrid").dataSource.page(0);
        $("#grid_details").data("kendoGrid").dataSource.page(0);
    }
</script>

<script class="grid_details">
    var detailsFields = [{
        title: "单据号",
        field: "OrderNo",
        template: "#=GetOperations(OrderNo)#",
        width: 140
    }, {
        title: "客户",
        field: "Corporation",
        width: 200
    }, {
        title: "日期",
        template: "#=kendo.toString(kendo.parseDate(NoteDate),'yyyy-MM-dd')#",
        width: 100
    }, {
        title: "产品",
        field: "ProductName",
        width: 300
    }, {
        title: "单价",
        field: "UnitPrice",
        format: "{0:c}",
        width: 150,
        attributes: { style: "text-align:right" }
    }, {
        title: "数量",
        field: "Quantity",
        template: "#=Quantity#(#=UnitName#) #=AssistantQuantity#",
        width: 100
    }, {
        title: "金额",
        field: "Money",
        format: "{0:c}",
        width: 150,
        attributes: { style: "text-align:right" }
    }, {}];

    $(function () {
        $("#grid_details").kendoGrid({
            columns: detailsFields,
            dataSource: Whale.dataSource({
                url: "/Orders/DealOrder/GetOrderDetails",
                getParameters: function () {
                    return {
                        getCustomer: true,
                        customerID: $("#txtCustomer").attr("data-value"),
                        orderDateBefore: $("#txtOrderDateBefore").val(),
                        orderDateAfter: $("#txtOrderDateAfter").val(),
                        orderNo: $("#txtKey").val(),
                        orderMethod: $("#ddlMethod").data("kendoDropDownList").value(),
                        orderStatus: $("#ddlOrderStatus").data("kendoMultiSelect").value().join(','),
                        productID: $("#txtProduct").attr("data-value")
                    };
                },
                pageSize: 20
            }),
            pageable: {
                messages: {
                    empty: "暂无数据"
                }
            },
            dataBound: function (e) {
                //让表头居中
                $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            }
        });
    });
</script>
